﻿<?
echo $_SESSION["session_message"];
$_SESSION["session_message"] = "";
?>
<table height="28" cellSpacing="0" cellPadding="0" width="100%" border="0">
      <tr align=center>
        <td class="title" width="100%">S&#7843;n ph&#7849;m : <a href="./?act=product_m&page=<? echo $page?>&cat=<? echo $_REQUEST['cat']; ?>"><font class="V10pt" color="#ffffff">Nh&#7853;p M&#7899;i</font></a>&nbsp;|&nbsp
	</td>
      </tr>
    </table>
<?
	switch ($_GET['action'])
	{
		case 'del' :
			$id = $_GET['id'];
			$pro=GetProductInfo($id);
			if ($pro)
			{
				$sql = "delete from products where products_id='".$id."'";
				$result = mysql_query($sql,$con);
				if ($result) 
				{
					if (file_exists($pro['products_image'])) @unlink($pro['products_image']);
					if (file_exists($pro['products_image_large'])) @unlink($pro['products_image_large']);
					
					mysql_query("delete from pro_good where products_id='".$id."'",$con);
					mysql_query("delete from pro_new where products_id='".$id."'",$con);
					
					echo "<p align=center class='err'>&#272;ã xóa thành công</p>";
				}
					else echo "<p align=center class='err'>Không th&#7875; xóa d&#7919; li&#7879;u</p>";
			}
			break;
	}
?>

<?
	if (isset($_POST['ButDel'])) {
		$cnt=0;
		foreach ($_POST['chk'] as $id)
		{
			$pro=GetProductInfo($id);
			if ($pro)
			{
				@$result = mysql_query("delete from products where products_id='".$id."'",$con);
				if ($result) {
					$cnt++;
					if (file_exists($pro['products_image'])) @unlink($pro['products_image']);
					if (file_exists($pro['products_image_large'])) @unlink($pro['products_image_large']);
					mysql_query("delete from pro_saleoff where products_id='".$id."'",$con);
					mysql_query("delete from pro_good where products_id='".$id."'",$con);
					mysql_query("delete from pro_new where products_id='".$id."'",$con);
						mysql_query("delete from pro_vp where products_id='".$id."'",$con);
				}
			}
		}
		echo "<p align=center class='err'>&#272;ã xóa ".$cnt." ph&#7847;n t&#7917;</p>";
	}
	if (isset($_POST['ButGood'])) {
		$cnt=0;
		foreach ($_POST['chk'] as $id)
		{
			$pro=GetProductInfo($id);
			if ($pro)
			{
				if (CountRecord("pro_good","products_id=".$pro['products_id'])<=0)
				{
					$result = mysql_query("insert into pro_good (products_id,pro_dateadded) values ('".$pro['products_id']."',SYSDATE())",$con);
					if ($result) {
						$cnt++;
					}
				}
			}
		}
		echo "<p align=center class='err'>&#272;ã c&#7853;p nh&#7853;t ".$cnt." ph&#7847;n t&#7917;</p>";
	}
	if (isset($_POST['Butnew'])) {
		$cnt=0;
		foreach ($_POST['chk'] as $id)
		{
			$pro=GetProductInfo($id);
			if ($pro)
			{
				if (CountRecord("pro_new","products_id=".$pro['products_id'])<=0)
				{
					$result = mysql_query("insert into pro_new (products_id,pro_dateadded) values ('".$pro['products_id']."',SYSDATE())",$con);
					if ($result) {
						$cnt++;
					}
				}
			}
		}
		echo "<p align=center class='err'>&#272;ã c&#7853;p nh&#7853;t ".$cnt." ph&#7847;n t&#7917;</p>";
	}
	if (isset($_POST['ButSaleoff'])) {
		$cnt=0;
		foreach ($_POST['chk'] as $id)
		{
			$pro=GetProductInfo($id);
			if ($pro)
			{
				if (CountRecord("pro_saleoff","products_id=".$pro['products_id'])<=0)
				{
					$result = mysql_query("insert into pro_saleoff (products_id,pro_dateadded) values ('".$pro['products_id']."',SYSDATE())",$con);
					if ($result) {
						$cnt++;
					}
				}
			}
		}
		echo "<p align=center class='err'>&#272;ã c&#7853;p nh&#7853;t ".$cnt." ph&#7847;n t&#7917;</p>";
	}
?>
<?
	
		if (isset($_POST['pro_vp'])) {
		$cnt=0;
		foreach ($_POST['chk'] as $id)
		{
			$pro=GetProductInfo($id);
			if ($pro)
			{
				if (CountRecord("pro_vp","products_id=".$pro['products_id'])<=0)
				{
					$result = mysql_query("insert into pro_vp (products_id,pro_dateadded) values ('".$pro['products_id']."',SYSDATE())",$con);
					if ($result) {
						$cnt++;
					}
				}
			}
		}
		echo "<p align=center class='err'>&#272;ã c&#7853;p nh&#7853;t ".$cnt." ph&#7847;n t&#7917;</p>";
	}
?>
<? 
	$page = $_REQUEST["page"];
	$p=0;
	if ($page!='') $p=$page;
	$where="1=1";
	if ($_REQUEST['cat']!='') $where="categories_id=".$_REQUEST['cat'];
?>
<form method="POST" name="frmList" action="index.php">
<input type=hidden name="page" value="<? echo $p; ?>">
<input type="hidden" name="act" value="<? echo $_REQUEST['act']; ?>">
<?
function taotrang($sql,$link,$nitem,$itemcurrent)
{	global $con;
	$ret="";
	$result = mysql_query($sql, $con) or die('Error' . mysql_error());
	$value = mysql_fetch_array($result);
	$plus = (($value['cnt'] % $nitem)>0);
	for ($i=0;$i<($value[0] / $nitem) + plus;$i++)
	{
		if ($i<>$itemcurrent) $ret .= "<a href=\"".$link.$i."\" class=\"lslink\">".($i+1)."</a> ";
		else $ret .= ($i+1)." ";
	}
	return $ret;
}
	$pageindex=taotrang("select count(*) from products where $where","./?act=product&cat=".$_REQUEST['cat']."&page=",$MAXPAGE,$page);
?>

<table cellspacing="0" cellpadding="0" width="100%">
<? if ($_REQUEST['code']==1) echo '<tr><td colspan="2" align="center" class="err">&#272;ã c&#7853;p nh&#7853;t thành công</td></tr>'; ?>
<tr>
<td class="smallfont">Trang : <? echo $pageindex; ?></td>
<td height="30" align="right" class="smallfont">
	<select size="1" name="ddCat" class="smallfont">
<?
	$ms=GetListCategory();
	echo '<option value="">[T&#7845;t c&#7843;]</option>';
	foreach ($ms as $m)
		if ($m[0]!=$_REQUEST['cat'])
			echo '<option value="'.$m[0].'">'.$m[1].'</option>';
		else
			echo '<option selected value="'.$m[0].'">'.$m[1].'</option>';
?>
	</select> 
	<input type="button" value="Chuy&#7875;n" class="button" onclick="window.location='./?act=product&cat='+ddCat.value">
	</td>
</tr>
</table>

<table border="1" cellpadding="2" style="border-collapse: collapse" bordercolor="#C9C9C9" width="100%" id="AutoNumber1">
  <tr>
    <td width="4%" align=center nowrap class="title"><input type="checkbox" name="chkall" onclick="chkallClick(this);"></td>
    <td colspan="2" nowrap class="title">&nbsp;</td>
    <td width="4%" align="center" nowrap class="title"><b>ID</b></td>
    <td width="7%" align="center" nowrap class="title"><b>Mã SP</b></td>
    <td width="18%" align="center" nowrap class="title"><b>Tên SP tiếng Việt </b></td>
	    <td width="18%" align="center" nowrap class="title"><b>Tên SP tiếng anh </b></td>

    <td width="24%" align="center" nowrap class="title"><b>Hình nh&#7887;</b></td>
    <td width="7%" align="center" nowrap class="title"><b>Giá</b></td>
	<td width="11%" align="center" nowrap class="title"><b>&#272;&#417;n v&#7883;</b></td>
    <td width="17%" align="center" nowrap class="title"><b>Danh m&#7909;c</b></td>
    <!--    <td align="center" nowrap class="title"><b>Ngôn ng&#7919;</b></td> -->
  </tr>
  
  <?php
           	$sql="select * from products where $where order by products_id desc limit ".($page*$MAXPAGE).",".$MAXPAGE;
        	$result=mysql_query($sql,$con);
        	$i=0;
           	while(($row=mysql_fetch_array($result)))
			{
			$i++;
			if ($i%2) $color="#d5d5d5"; else $color="#e5e5e5";
			$catinfo=GetCategoryInfo($row['categories_id']);
			$providerinfo=GetProviderInfo($row['providers_id']);
			$donvi=GetDonViInfo($row['products_donvi']);
  ?>
  
  <tr>
    <td align="center" bgcolor="<? echo $color; ?>" class="smallfont">
    <input type="checkbox" name="chk[]" value="<? echo $row['products_id']; ?>"></td>
    <td width="4%" align="center" bgcolor="<? echo $color; ?>" class="smallfont">
    <a href="./?act=product_m&cat=<? echo $_REQUEST['cat']; ?>&id=<? echo $row['products_id']; ?>&page=<? echo $page?>">S&#7917;a</a></td>
    <td width="4%" align="center" bgcolor="<? echo $color; ?>" class="smallfont">
    <a onclick="return confirm('B&#7841;n có ch&#7855;c ch&#7855;n mu&#7889;n xoá ?');" href="./?act=product&action=del&cat=<? echo $_REQUEST['cat']; ?>&id=<? echo $row['products_id']; ?>">Xoá</a></td>
    <td bgcolor="<? echo $color; ?>"  align="left" class="smallfont"><? echo $row['products_id']; ?>&nbsp;</td>
    <td bgcolor="<? echo $color; ?>" class="smallfont"><? echo $row['products_code']; ?>&nbsp;</td>
    <td bgcolor="<? echo $color; ?>" class="smallfont"><? echo $row['products_name']; ?>&nbsp;</td>
	    <td bgcolor="<? echo $color; ?>" class="smallfont"><? echo $row['products_include']; ?>&nbsp;</td>
    <td bgcolor="<? echo $color; ?>" class="smallfont"><img src="<? echo $row['products_image']; ?>" / height="50"></td>
    <td bgcolor="<? echo $color; ?>" class="smallfont"><? echo $row['products_price']; ?>&nbsp;</td>
	<td bgcolor="<? echo $color; ?>" class="smallfont"><? echo $donvi['dv_name']; ?>&nbsp;</td>
    <td bgcolor="<? echo $color; ?>" class="smallfont"><? echo $catinfo['categories_name']; ?>&nbsp;</td>
    <!--   <td bgcolor="<?echo $color; ?>" class="smallfont"><?echo $row['language']; ?>&nbsp;</td> -->
  </tr>
  <?
              	}
  ?>
</table>
<table border="0" width="100%" cellspacing="0" cellpadding="0" id="table1">
	<tr>
		<td>
<input type="submit" value="Xóa Ch&#7885;n" name="ButDel" onclick="return confirm('B&#7841;n có ch&#7855;c ch&#7855;n mu&#7889;n xoá ?');" class="button"></td>
		<td align="right">&nbsp;
</td>
		<td align="right">&nbsp;
</td>
<td align="right">

</td>



	</tr>
</table>
</form>
<script language="JavaScript">
function chkallClick(o) {
  	var form = document.frmList;
	for (var i = 0; i < form.elements.length; i++) {
		if (form.elements[i].type == "checkbox" && form.elements[i].name!="chkall") {
			form.elements[i].checked = document.frmList.chkall.checked;
		}
	}
}
</script>